from msilib.schema import tables
import db_helper


def getAllTable():
    sql = ''' select table_name ,create_time , engine, table_collation, table_comment from information_schema.tables where table_schema = (select database()) order by create_time desc '''
    with db_helper.buildDbHelper(user='mysql', database='IntermediaryMedia') as db:
        tables = db.executeQuerySql(sql)
        print(tables)
        # for table in tables:
        #     print(table[0])
        return tables


def buildEntity(table, columns):
    tabName = table[0]
    colGeneratedValue=''
    colDate=''
    colBigDecimal=''
    bodyEntity = f'''  '''

    for c in columns:
        cName = str(c[0])
        clName = cName[:1].lower()+cName[1:]
        colType = str(c[2])
        colPri = str(c[4])
        colPriType = str(c[5])
        bodyPart = ''' '''
        bodyType = ''
        if colPri == 'PRI':
            bodyPart += f''' @Id '''
            if colPriType == 'auto_increment':
                bodyPart += f''' @GeneratedValue '''
                colGeneratedValue='import jakarta.persistence.GeneratedValue;'
        if colType == "int":
            bodyType = 'Integer'
        elif colType == "varchar":
            bodyType = 'String'
        elif colType == "datetime":
            bodyType = 'Date'
            colDate ='import java.util.Date;'
        elif colType == "decimal":
            bodyType = 'BigDecimal'
            colBigDecimal='import java.math.BigDecimal;'

        bodyPart += f''' @Column(name="{cName}") private {bodyType} {clName};  
        public {bodyType} get{cName}() {{ return {clName}; }}
        public void set{cName}({bodyType} {clName}) {{ this.{clName} = {clName};}} '''

        bodyEntity += bodyPart
    
    startEntity = f''' package com.gold.intermediary.model;
                    import jakarta.persistence.Column;
                    {colGeneratedValue}
                    import jakarta.persistence.Entity;
                    import jakarta.persistence.Id;
                    import jakarta.persistence.Table; 
                    import java.io.Serializable;
                    {colBigDecimal}
                    {colDate}
                    @Entity
                    @Table(name="{tabName}")
                    public class {tabName} implements Serializable {{
                    '''
    endEntity = f''' }} '''
    entityStr = startEntity + bodyEntity+endEntity
    fileName = f'''entity\{tabName}.java'''
    f = open(fileName, "x")
    f.write(entityStr)
    f.close()
    pass


def getTableInfo(tables):
    with db_helper.buildDbHelper(user='mysql', database='IntermediaryMedia') as db:
        for table in tables:
            sql = f''' select column_name, is_nullable, data_type, column_comment, column_key, extra from information_schema.columns where table_name = '{str(table[0])}' and table_schema = (select database()) order by ordinal_position '''
            columns = db.executeQuerySql(sql)
            print(columns)
            buildEntity(table, columns)
    pass


def main():
    tables = getAllTable()
    getTableInfo(tables)
    pass


if __name__ == '__main__':
    main()
    # fileName = f'''entity\\abc.java'''
    # f = open(fileName, "x")
    # f.write('''entity\\entityStr''')
    # f.close()
